﻿<%@ WebHandler Language="C#" Class="file" %>

using System;
using System.Web;
using System.IO;
using System.Configuration;
using CE.Database;
using System.Data;
public class file : IHttpHandler
{
    string FileName;
    public void ProcessRequest(HttpContext context)
    {
        this.FileName = context.Request.Params["docrefno"];
        if (this.FileName != null)
        {
            MyDatabase db = new MyDatabase();
            DataTable dtFile = db.GetDataTable("SELECT	dbo.udfnGetFileName([FL].[FileName]) AS [FileName],FL.OriginalName,[FL].[FileName] AS FilePath, FL.FileTypeId,FL.FileId,UT.EN FileType,FL.CECarId "
				+ "FROM	tblNonGGCarFileUploadDetails FL INNER JOIN tblFileUploadType UT ON UT.FileTypeId = FL.FileTypeId "
				+ "WHERE	[FL].DocRefNo Like '"+this.FileName +"'");
            if (dtFile != null && dtFile.Rows.Count > 0)
            {
                string path = dtFile.Rows[0]["FilePath"].ToString();
                this.FileName = dtFile.Rows[0]["FileType"].ToString() + "_" + dtFile.Rows[0]["CECarID"].ToString();

                FileInfo fInfo = new FileInfo(path);
                if (fInfo.Exists)
                {
                    FileStream file = new FileStream(path, FileMode.Open);
                    byte[] buffer = new byte[(int)file.Length];
                    file.Read(buffer, 0, (int)file.Length);
                    file.Close();
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.AddHeader("content-disposition", "attachment; filename=\"" + this.FileName + fInfo.Extension.ToString() + "\"");
                    context.Response.BinaryWrite(buffer);
                    context.Response.End();
                }
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}